<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "category".
 *
 * @property int $id ID
 * @property string $name 分类名称
 * @property int $parent 上级分类id
 * @property int $grade 分类层级1：一级分类，2：二级分类，3：三级分类
 * @property string $keywords 关键词
 * @property string $description 描述
 * @property string $addtime 添加时间
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'parent', 'addtime'], 'required'],
            [['parent', 'grade', 'addtime'], 'integer'],
            [['name'], 'string', 'max' => 25],
            [['keywords'], 'string', 'max' => 300],
            [['description'], 'string', 'max' => 1000],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '分类名称',
            'parent' => '上级分类id',
            'grade' => '分类层级1：一级分类，2：二级分类，3：三级分类',
            'keywords' => '关键词',
            'description' => '描述',
            'addtime' => '添加时间',
        ];
    }
    
    /***
     * 获取所有分类
     */
    public static function getTopCategories(){
        $sql = "select * from category where parent=0";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    
    public static function getSubCate($parent){
        $sql = "select id,name,parent,grade from category where parent=$parent";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    public static function getCateName($cateid){
        $sql = "select name from category where id=$cateid";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryOne();
        return $model['name'];
    }
    
    public static function getTopCate(){
        $sql = "select id,name from category where parent=0";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    public static function getIndexpageCate(){
        $sql = "select id,name from category where parent=0 and id!=12";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    public static function getStoreTopCate(){
        $sql = "select id,name from category where parent=0 and id != 12";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    public static function getWorkerTopCate(){
        $sql = "select id,name from category where parent=0 and id = 12";
        $command = \Yii::$app->db->createCommand($sql);
    	return $model = $command->queryAll();
    }
    
    
    public static function getAllCate(){
        $data = self::getTopCategories();
        foreach($data as $first_key=>$first_cate){
            $sub = self::getSubCate($first_cate['id']);
            foreach ($sub as $second_key=>$second_cate){
                $sub[$second_key]['sub'] = self::getSubCate($second_cate['id']);
            }
            $data[$first_key]['sub'] = $sub;
        }
        return $data;
    }
    
    
    public static function getAdCate(){
        return self::getSubCate(1);
    }
    
    public static function getDeviceCate(){
        return self::getSubCate(7);
    }
    
    public static function getNameById($id){
        $sql = "select name from category where id=$id";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryOne();
        return $model['name'];
    }
    
    public static function getKeyValue(){
        $sql = "select id,name from category";
        $command = \Yii::$app->db->createCommand($sql);
    	$model = $command->queryAll();
        return ArrayHelper::column($model, "name","id");
    }
}
